#ifndef __UDP_SERVER__HPP
#define __UPD_SERVER__HPP

#include <iostream>
#include <string>
#include <cstring>
#include <memory>
#include <string.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>


const static int gsockfd = -1;
const std::string gdefaultip = "127.0.0.1";
const static uint16_t gdefaultport = 8888;

class UdpServer
{
private:
    int _sockfd;
    uint16_t _port;  //服务器端口号
    bool _isrunning; //服务器运行状态
public:
    //UdpServer(const std::string ip = gdefaultip, const uint16_t port = gdefaultport)
    UdpServer(const uint16_t newport = gdefaultport)
        :_sockfd(gsockfd)
        ,_port(newport)
        ,_isrunning(false)
    {
        
    }

    void InitServer()
    {
        // 1.创建套接字
        _sockfd = ::socket(AF_INET, SOCK_DGRAM, 0);
        if(_sockfd < 0)
        {
            std::cout << "socket:" << strerror(errno) << std::endl;
            exit(1);
        }
        std::cout << "socket success, sockfd is: " << _sockfd << std::endl;
        
        // 2、填充网络信息，
        // 2.1 没有把socket信息，设置进如内核中，只是填充了结构体!
        struct sockaddr_in loacl;
        memset(&loacl, 0, sizeof(loacl));
        loacl.sin_family = AF_INET;
        loacl.sin_port = ::htons(_port); //要被发送给对方的，即要发送到网络中！
        loacl.sin_addr.s_addr = INADDR_ANY;
        //loacl.sin_addr.s_addr = ::inet_addr(_ip.c_str()); //1. 点分十进制转为整数 2. 转成网络字节序

        // 2.2 bind 设置如内核中
        int n = ::bind(_sockfd, (struct sockaddr*)&loacl, sizeof(loacl));
        if(n < 0)
        {
            std::cout << "bind: "<< errno << " " <<strerror(errno) << std::endl;
            exit(2);
        }
        std::cout << "bind success" << std::endl;
    }

    void Start()
    {
        _isrunning = true;
        int count = 0;
        while(true)
        {
            char inbuffer[1024];
            struct sockaddr_in peer;
            socklen_t len = sizeof(peer); //必须设定
            //std::cout<< count++<<std::endl;
            //std::cout << "recvfrom..." << std::endl;
            ssize_t n = ::recvfrom(_sockfd, inbuffer, sizeof(inbuffer)-1, 0, (struct sockaddr*)(&peer), &len);
            //std::cout << "recvfrom: " << n << std::endl;
            if (n > 0)
            {
                // 在后面字符串后面加上反斜杠0，以便于输出
                inbuffer[n] = 0;

                // 1.消息内容 && 2.谁发给我的
                uint16_t clientport = ::ntohs(peer.sin_port);
                std::string clientip = ::inet_ntoa(peer.sin_addr);

                std::string clientinfo = clientip + ":" + std::to_string(clientport) + "# " + inbuffer;
                std::cout << clientinfo << std::endl;

                std::string echo_str = "echo# ";
                echo_str += inbuffer;
                ::sendto(_sockfd, echo_str.c_str(), echo_str.size(), 0, (struct sockaddr*)(&peer), sizeof(peer));
            }
        }
        _isrunning = false;
    }

    ~UdpServer()
    {
        if (_sockfd > gsockfd)
            ::close(_sockfd);
    }

};

#endif